<?php
/**
 * SKU Model
 *
 * PHP Version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     FarmerLi <lixu_01@126.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN:$Id: projects.php 3051 2011-01-11 13:06:18Z sunbo $
 */

namespace Project\Apps\Ecga\Models;

use \Project\Models\Abstraction\Ecga\Skus as bizSkus;

/**
 * SKU Model
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    FarmerLi <lixu_01@126.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: 0.1
 */
class Skus extends bizSkus
{
    /**
     * 获取指定SKU列表
     *
     * @param array $skus SKU列表
     *
     * @return iterator
     */
    public function getBySkus($skus)
    {
        $return = $this->select()
            ->where('sku', 'in', $skus)
            ->fetch();
        return $return;
    }
    
    /**
     * 获取指定SKU
     *
     * @param array $sku SKU
     *
     * @return object
     */
    public function getBySKU($sku)
    {
        return $this->select()
            ->where('sku', '=', $sku)
            ->fetch()
            ->current();
    }
    
    /**
     * 按ProjectId 获取SKU
     * 
     * @param 项目Id $projectId 项目Id
     * 
     * @return iterator
     */
    public function getByProjectId($projectId)
    {
        $return = $this->select()
            ->where('projectId', '=', $projectId)
            ->fetch();
        return $return;
    }
    /**
     * 按星级 获取SKU
     * 
     * @param int $level 星级
     * 
     * @return string
     */
    public function getSkuByLevel($level) 
    {
        $return = array();
        $result = $this->select('sku')
            ->where('level', '=', $level)
            ->fetch();
        foreach ($result as $value) {
            $return[] = $value['sku'];
        }
        return $return;
    }
    /**
     * 按模型 获取SKU
     * 
     * @param string $model 模型名称
     * 
     * @return string
     */
    public function getSkuByModel($model) 
    {
        $return = array();
        $result = $this->select('sku')
            ->where('model', 'like', '%' . $model . '%')
            ->fetch();
        foreach ($result as $value) {
            $return[] = $value['sku'];
        }
        return $return;
    }
    
    /**
     * 获取项目下的所有Model 用于select下拉
     * 
     * @param int $projectId 项目ID
     * 
     * @return array
     */
    public function getModels($projectId = 1)
    {
        $result = $this->select('model')
            ->where('projectId', '=', $projectId)
            ->groupBy('model')
            ->fetch();
        return $result;
    }
    
    /**
     * 取分页query
     *
     * @param array $filter 过滤器
     *
     * @return query $query
     * @author sunfengming
     */
    public static function getPageQuery($filter = array())
    {
        $model = model('Skus');
        $filterInstance = $model->filter();
        if ($filter['title'] == '') {
            unset($filter['title']);
        }
        if ($filter['sku'] == '') {
            unset($filter['sku']);
        }
        if ($filter['model'] == '') {
            unset($filter['model']);
        }
        if ($filter['level'] == '') {
            unset($filter['level']);
        }
        foreach ($filter as $key => $value) {
            if (in_array($key, array('title', 'sku', 'model', 'level'))) {
                $filterInstance->where($key, 'like', '%' . $value . '%');
            } else {
                $filterInstance->where($key, '=', $value);
            }
        }
        
        return $model
            ->query($filterInstance)
            ->fields('*');
    }
}